//
//  CTShareView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/6.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTShareView.h"
#import "CTRemindView.h"
#import "CTHeader.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTSetView_Def.h"
#import "UIViewAdditions.h"
#import "CTRemindView.h"

#define EDIT_GIF_SHARE_COLOR [UIColor colorWithRed:250/255.0 green:250/255.0 blue:240/255.0 alpha:1.0]
#define SHAREVIEW_TAG_QQ        1000
#define SHAREVIEW_TAG_WECHAT    1001
#define SHAREVIEW_TAG_SINA      1002
#define SHAREVIEW_TAG_MESSAGE   1003
#define SHAREVIEW_TAG_MORE      1004
#define SHAREVIEW_TAG_LIKE      1005

#define SHAREVIEW_TAG_PIC_WECHATFRIENDS 1003
#define SHAREVIEW_TAG_PIC_QQSPACE       1004
#define SHAREVIEW_TAG_PIC_SINA          1002
#define SHAREVIEW_TAG_PIC_WECHAT        1000
#define SHAREVIEW_TAG_PIC_QQ            1001
//#define SHAREVIEW_TAG_PIC_MORE          1005
#define SHAREVIEW_TAG_PIC_SAVE          1005

typedef enum{
    CTShareViewMoveTypeUp = 1,
    CTshareViewMoveTypeDown,
}CTshareViewMoveType;

@interface CTShareView()
{
    CTShareViewType shareViewType;
    UIView          *backgroundView;
    UIView          *foregroundView;
    UIImageView     *backgroundImageView;
    UIImageView     *gifImageViewbg;
    UIImageView     *gifImageView;
    UIButton        *gifBtnLeft;
    UIButton        *gifBtnRight;
    UIView          *shareView;
}

@end


@implementation CTShareView
@synthesize imageIndex,imageListArray,delegate;


- (void)dealloc
{
    [backgroundView removeFromSuperview];
    backgroundView = nil;
    [foregroundView removeFromSuperview];
    foregroundView = nil;
    backgroundImageView = nil;
    [backgroundView removeFromSuperview];
    gifImageViewbg = nil;
    [gifImageView removeFromSuperview];
    gifImageView   = nil;
    [gifBtnLeft removeFromSuperview];
    gifBtnLeft     = nil;
    [gifBtnRight removeFromSuperview];
    gifBtnRight    = nil;
    [shareView removeFromSuperview];
    shareView      = nil;
    delegate = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTShareView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
    
        self.backgroundColor = [UIColor clearColor];
        [self createShareView];
    }
    return self;
}


-(void)showShareViewOn:(UIView *)view withShareViewType:(CTShareViewType)type
{
    if (view == nil)
    {
        return;
    }
    [view addSubview:self];
    shareViewType = type;
    
    if (imageIndex > imageListArray.count || imageIndex < 0) {
        return;
    }
    ////////
    if (imageIndex == imageListArray.count)
    {
        
    }
    
    if (imageIndex == 0)
    {
        
    }
    
    //判断View类型设置foregroundView大小
    if (type == CTShareViewTypeDefault)
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
        [self addGestureRecognizer:tap];

        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT)];
        backgroundView.backgroundColor = [UIColor clearColor];
//        backgroundView.alpha = 0.8;
        [self addSubview:backgroundView];
        [self addSubview:foregroundView];
        [foregroundView addSubview:shareView];

        foregroundView.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT, CT_HEADER_SCREEN_WIDTH, 195);
        shareView.frame = CGRectMake(35, 0, CT_HEADER_SCREEN_WIDTH-70, 195);
        
        NSArray *nameAry = [NSArray arrayWithObjects:
                            [CTAppLanguage localizedStringForKey:@"shareView_wechat" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_qq" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_sina" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_wechat_friends" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_qq_space" withValue:nil withTable:@"Language"],
                            nil];
        NSArray *imageAry = [NSArray arrayWithObjects:@"wechat",@"qq",@"sina",@"pengyouquan",@"kongjian", nil];
        NSArray *imageAryClick = [NSArray arrayWithObjects:@"wechat_click",@"qq_click",@"sina_click",@"pengyouquan_click",@"kongjian_click", nil];
        
        int n = 0;
        for (int i = 0; i < 2; i++)
        {
            
            for (int j = 0; j < 3; j++)
            {
                if (n < 5)
                {
                    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                    btn.frame = CGRectMake(j*40+j*((shareView.width-120)/2), 25+i*81, 40, 40);
                    [btn addTarget:self action:@selector(btnPicAction:) forControlEvents:UIControlEventTouchUpInside];
                    [btn setBackgroundImage:[UIImage imageNamed:imageAry[n]] forState:UIControlStateNormal];
                    [btn setBackgroundImage:[UIImage imageNamed:imageAryClick[n]] forState:UIControlStateHighlighted];
                    btn.tag = 1000+n;
                    [shareView addSubview:btn];
                    
                    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(btn.origin.x-10, btn.bottom+8, btn.width+20, 14)];
                    label.text = nameAry[n];
                    label.textAlignment = NSTextAlignmentCenter;
                    label.textColor =SET_LABEL_TEXT_COLOR;
                    label.font = [UIFont fontWithName:nil size:13];
                    [shareView addSubview:label];
                    n++;
                }
            }
        }
        
    }
    else if (type == CTShareViewTypeFree)
    {
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 27.5, (CT_HEADER_SCREEN_WIDTH-70)/2, 0.5)];
        leftLabel.backgroundColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
        [foregroundView addSubview:leftLabel];
    
        UILabel *btnLabel = [[UILabel alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-70)/2, 20, 70, 15)];
        btnLabel.text = [CTAppLanguage localizedStringForKey:@"shareView_closeBtn_title" withValue:nil withTable:@"Language"];
        btnLabel.textColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
        btnLabel.font = [UIFont fontWithName:nil size:15];
        btnLabel.textAlignment = NSTextAlignmentCenter;
        [foregroundView addSubview:btnLabel];
    
        UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(btnLabel.right, 27.5, (CT_HEADER_SCREEN_WIDTH-70)/2, 0.5)];
        rightLabel.backgroundColor = [UIColor colorWithRed:164/255.0 green:164/255.0 blue:164/255.0 alpha:1.0];
        [foregroundView addSubview:rightLabel];

        self.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT-333, CT_HEADER_SCREEN_WIDTH, 270);
        backgroundImageView.hidden = YES;
        [self addSubview:foregroundView];
        [foregroundView addSubview:shareView];
        foregroundView.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 270);
        shareView.frame = CGRectMake(35, 40, CT_HEADER_SCREEN_WIDTH-70, 180);
        
        
        NSArray *nameAry = [NSArray arrayWithObjects:
                            [CTAppLanguage localizedStringForKey:@"shareView_wechat" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_qq" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_sina" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_wechat_friends" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_qq_space" withValue:nil withTable:@"Language"],
                            [CTAppLanguage localizedStringForKey:@"shareView_save" withValue:nil withTable:@"Language"]
                             ,nil];
        NSArray *imageAry = [NSArray arrayWithObjects:@"wechat",@"qq",@"sina",@"pengyouquan",@"kongjian", @"save",nil];
        NSArray *imageAryClick = [NSArray arrayWithObjects:@"wechat_click",@"qq_click",@"sina_click",@"pengyouquan_click",@"kongjian_click",@"save_click", nil];
        int n = 0;
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (n < 6)
                {
                    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                    btn.frame = CGRectMake(j*40+j*((shareView.width-120)/2), 20+i*81, 40, 40);
                    [btn addTarget:self action:@selector(btnPicAction:) forControlEvents:UIControlEventTouchUpInside];
                    [btn setBackgroundImage:[UIImage imageNamed:imageAry[n]] forState:UIControlStateNormal];
                    [btn setBackgroundImage:[UIImage imageNamed:imageAryClick[n]] forState:UIControlStateHighlighted];
                    btn.tag = 1000+n;
                    [shareView addSubview:btn];
                    
                    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(btn.origin.x-10, btn.bottom+8, btn.width+20, 14)];
                    label.text = nameAry[n];
                    label.textAlignment = NSTextAlignmentCenter;
                    label.textColor =SET_LABEL_TEXT_COLOR;
                    label.font = [UIFont fontWithName:nil size:13];
                    [shareView addSubview:label];
                    n++;
                }
            }
        }
    }
    else
    {
        [CTDebugManager showDebugLogInfo:@"CTShareViewType 类型不匹配 ！！！"];
    }
    
    if (type == CTShareViewTypeDefault)
    {
        
        [self giveUpAction:CTShareViewMoveTypeUp withInt:195];
    }
    else if (type == CTShareViewTypeFree)
    {

        UIButton * sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sendBtn.frame = CGRectMake(0, foregroundView.height-50, CT_HEADER_SCREEN_WIDTH, 50);
        [sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        sendBtn.backgroundColor = CT_HEADER_GREEN_COLOR;

        [sendBtn setTitle:[CTAppLanguage localizedStringForKey:@"shareView_free_btn_title" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
        [sendBtn setTitleColor:CT_HEADER_WHITE_COLOR forState:UIControlStateNormal];
        [foregroundView addSubview:sendBtn];
        
//        CALayer *btnlayer = [saveBtn layer];
//        btnlayer.borderColor = [[UIColor colorWithRed:255/255.0 green:161/255.0 blue:106/255.0 alpha:1.0] CGColor];
//        btnlayer.borderWidth = 1.0f;
    }
    else
    {
        
    }
    
}

//发布到应用内
- (void)sendBtnAction:(UIButton *) sender
{
//    - (void)shareViewShareToSave:(CTShareView *)view withImage:(UIImage *)image;
    if ([self.delegate respondsToSelector:@selector(shareViewSharePicoPico:)])
    {
        [self.delegate shareViewSharePicoPico:self];
    }
}
- (void)createShareView
{

    foregroundView = [[UIView alloc]init];
    foregroundView.backgroundColor = CT_HEADER_WHITE_COLOR;
    shareView = [[UIView alloc]init];
    [foregroundView addSubview:shareView];
}

-(void)removeShareView
{
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(viewDidRemoved:)])
    {
        [self.delegate viewDidRemoved:self];
    }
}
//划动事件
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
        
        //执行程序
    }
    if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
        
        //执行程序
    }
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        
        imageIndex++;
        if (imageIndex >= imageListArray.count-1)
        {
            imageIndex = imageListArray.count-1;
            CTRemindView *remindView = [[CTRemindView alloc]init];
            [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"shareView_last_text" withValue:nil withTable:@"Language"]];
        }
        gifImageView.image = [UIImage imageNamed:imageListArray[imageIndex]];
        
    }
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        
        imageIndex--;
        if (imageIndex <= 0)
        {
            imageIndex = 0;
            CTRemindView *remindView = [[CTRemindView alloc]init];
            [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"shareView_first_text" withValue:nil withTable:@"Language"]];
        }
        gifImageView.image = [UIImage imageNamed:imageListArray[imageIndex]];
        
    }
}
- (void)tapGestureAction:(UITapGestureRecognizer *) sender
{
    [self giveUpAction:CTshareViewMoveTypeDown withInt:270];
}
//关闭按钮
- (void)closeBtnAction:(UIButton *) sender
{
    if (shareViewType == CTShareViewTypeDefault)
    {
        [self giveUpAction:CTshareViewMoveTypeDown withInt:375];

    }
    else if (shareViewType == CTShareViewTypeFree)
    {
        [self giveUpAction:CTshareViewMoveTypeDown withInt:215];

    }
    else
    {
        
    }
//    [self removeShareView];
}
//左按钮
- (void)gifBtnLeftAction:(UIButton *) sender
{
    imageIndex--;
    if (imageIndex <= 0)
    {
        imageIndex = 0;
        CTRemindView *remindView = [[CTRemindView alloc]init];
        [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"shareView_first_text" withValue:nil withTable:@"Language"]];
    }
    gifImageView.image = [UIImage imageNamed:imageListArray[imageIndex]];
}
//右按钮
- (void)gifBtnRightAction:(UIButton *) sender
{
    imageIndex++;
    if (imageIndex >= imageListArray.count-1)
    {
        imageIndex = imageListArray.count-1;
        CTRemindView *remindView = [[CTRemindView alloc]init];
        [remindView showRemindViewOn:self withRemindWord:[CTAppLanguage localizedStringForKey:@"shareView_last_text" withValue:nil withTable:@"Language"]];
    }
    gifImageView.image = [UIImage imageNamed:imageListArray[imageIndex]];
}
//表情事件
- (void)btnAction:(UIButton *) sender
{
    switch (sender.tag)
    {
        case SHAREVIEW_TAG_QQ:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToQQ:withImage:)])
            {
                [self.delegate shareViewShareToQQ:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
        case SHAREVIEW_TAG_WECHAT:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToWeChat:withImage:)])
            {
                [self.delegate shareViewShareToWeChat:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
        case SHAREVIEW_TAG_SINA:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToSina:withImage:)])
            {
                [self.delegate shareViewShareToSina:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
        case SHAREVIEW_TAG_MESSAGE:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToMessage:withImage:)])
            {
                [self.delegate shareViewShareToMessage:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
        case SHAREVIEW_TAG_MORE:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToMore:withImage:)])
            {
                [self.delegate shareViewShareToMore:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
        case SHAREVIEW_TAG_LIKE:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToCollcation:withImage:)])
            {
                [self.delegate shareViewShareToCollcation:self withImage:[UIImage imageNamed:imageListArray[imageIndex]]];
            }
            break;
            
        default:
            break;
    }
}



//配图
- (void)btnPicAction:(UIButton *) sender
{
    switch (sender.tag)
    {
        case SHAREVIEW_TAG_PIC_WECHAT:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToWeChat:withImage:)])
            {
                [self.delegate shareViewShareToWeChat:self withImage:nil];
            }
            break;
        case SHAREVIEW_TAG_PIC_QQ:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToQQ:withImage:)])
            {
                [self.delegate shareViewShareToQQ:self withImage:nil];
            }
            break;
        case SHAREVIEW_TAG_PIC_SINA:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToSina:withImage:)])
            {
                [self.delegate shareViewShareToSina:self withImage:nil];
            }

            break;
        case SHAREVIEW_TAG_PIC_WECHATFRIENDS:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToWechatFriends:withImage:)])
            {
                [self.delegate shareViewShareToWechatFriends:self withImage:nil];
            }
            break;
        case SHAREVIEW_TAG_PIC_QQSPACE:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToQQSpace:withImage:)])
            {
                [self.delegate shareViewShareToQQSpace:self withImage:nil];
            }
            break;
//        case SHAREVIEW_TAG_PIC_MORE:
//            if ([self.delegate respondsToSelector:@selector(shareViewShareToMore:withImage:)])
//            {
//                [self.delegate shareViewShareToMore:self withImage:nil];
//            }
//            break;
        case SHAREVIEW_TAG_PIC_SAVE:
            if ([self.delegate respondsToSelector:@selector(shareViewShareToSave:withImage:)])
            {
                [self.delegate shareViewShareToSave:self withImage:nil];
            }
            break;
            
        default:
            break;
    }
}

//弹出收回动画
-(void)giveUpAction:(CTshareViewMoveType)type withInt:(int )int_h
{
    
    if (type == CTShareViewMoveTypeUp)
    {
        [UIView animateWithDuration:0.3f animations:^
         {
             foregroundView.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT-int_h, CT_HEADER_SCREEN_WIDTH, int_h);
             
         }completion:^(BOOL finished){
         }];
    }
    else
    {
        [UIView animateWithDuration:0.3f animations:^
         {
             foregroundView.frame = CGRectMake(0, CT_HEADER_SCREEN_HEIGHT, CT_HEADER_SCREEN_WIDTH, int_h);
             
         }completion:^(BOOL finished)
         {
             [self removeShareView];
         }];
    }
    
}

@end

